/**
 * Created by 黄敏 on 2016/10/17.
 */



$(function () {

    var header = $('#book-header'); //部头
    var nav = $('#book-nav');   //导航
    var main = $('#book-main'); //主体
    var catalogue = $('#book-catalogue'); //目录窗
    var selectSize = $('#select-size'); //字号弹窗
    var _={};
    main.readBook(firstLoad);

    //首次加载目录
    function firstLoad(data,goBack){
        _ = goBack;
        $('h1').html(data.book.bookname);

        var html = '';
        data.catalog.forEach(function(val,k){
            html += '<li data-index ="'+k+'" ><a href="javascript:;">('+k+') '+val.title+'</a></li>'
        });

        $('#book-catalogue').find('ul').html(html);
        muluload();
        updatePageTitle()
    }

    //正文事件
    !function (obj) {
        var devWidth = main.outerWidth(true); //屏幕宽度
        var touchX = 0, touchY = 0, touchD = 0, orientation = 0;
        var centerWidth = devWidth * 0.35;
        var rightWidth = devWidth * 0.65;
        obj.on('touchstart', function (event) {
            touchD = 0; //恢复触屏误差值

            event.preventDefault();// 阻止浏览器默认事件，重要
            var touch = event.targetTouches[0];

            touchX = touch.clientX;
            touchY = touch.clientY;
        });

        obj.on('touchmove', function (event) {
            event.preventDefault();// 阻止浏览器默认事件，重要
            var touch = event.targetTouches[0];
            var x = touch.clientX;
            var y = touch.clientY;
            orientation = x - touchX; //方向，正数为右，负数为左；
            var DX = Math.abs(orientation);
            var DY = Math.abs(y - touchY);

            touchD = DX > DY ? DX : DY; //计算差值
        });

        obj.on('touchend', function (event) {
            event.preventDefault();// 阻止浏览器默认事件，重要
            if (header.hasClass('active')) {
                selectSize.removeClass('active');
                header.removeClass('active');
                nav.removeClass('active');
                return false;
            }

            if (touchD < 7) {
                console.warn('差值较小，属于单击事件');
                if (touchX <= centerWidth) {
                    console.log('上一页');
                    _.prevPageFun(updatePageTitle);

                } else if (touchX >= rightWidth) {
                    console.log('下一页');
                    _.nextPageFun(updatePageTitle);
                } else {
                    if (!header.hasClass('active')) {
                        header.addClass('active');
                        nav.addClass('active')
                    } else {
                        header.removeClass('active');
                        selectSize.removeClass('active');
                        nav.removeClass('active')
                    }
                }

            } else {

                console.info('差值较大，属于滑屏事件');

                if (orientation >= 0) {

                    console.log('向右滑，上一页');
                    _.prevPageFun(updatePageTitle);

                } else {

                    console.log('向左滑，下一页');
                    _.nextPageFun(updatePageTitle);

                }
            }
        });
    }(main);

    //导航按钮
    !function (obj) {
        var night = obj.find('a.nav-night'); //夜间模式切换
        var cata = obj.find('a.nav-catalog'); //目录切换
        var font = obj.find('a.nav-fontSize');//字号

        var isNight = $.cookie('isNight');
        if (isNight == 'true') {
            main.addClass('tonight');
            night.find('i').removeClass('icon-baitianmoshi').addClass('icon-yejian');
            night.find('span').html('夜间');
        }

        //夜间模式切换
        night.on('touchend', function (event) {
            main.addClass('transition');

            event.preventDefault();// 阻止浏览器默认事件
            if (main.hasClass('tonight')) {
                $.cookie('isNight', 'false', {expires: 9999});
                console.log('白天模式');
                main.removeClass('tonight');
                night.find('i').removeClass('icon-yejian').addClass('icon-baitianmoshi');
                night.find('span').html('白天');
            } else {
                $.cookie('isNight', 'true', {expires: 9999});
                console.log('夜间模式');
                main.addClass('tonight');
                night.find('i').removeClass('icon-baitianmoshi').addClass('icon-yejian');
                night.find('span').html('夜间');
            }
            setTimeout(function () {
                main.removeClass('transition');
            }, 400);
        });

        //展开目录
        cata.on('touchend', function () {
            catalogue.addClass('active');
        });
        catalogue.find('div.close-catalogue').on('touchend', function () {
            catalogue.removeClass('active');
        });

        //字号
        font.on('touchend',function(){
                console.log('123456789');
            if(selectSize.hasClass('active')){
                selectSize.removeClass('active');
            }else{
                selectSize.addClass('active');
            }}

        );
    }(nav);

    //首次提示
    // !function () {
    //     var skip = $('ul.skip-page');
    //     var isSecond = $.cookie('isSecond');
    //
    //     if(isSecond === undefined){
    //         skip.addClass('active');
    //         //去除提示
    //         skip.on('touchend', removeTipFun);
    //         function removeTipFun() {
    //             var parent = $(this);
    //             parent.removeClass('active');
    //             header.removeClass('active');
    //             nav.removeClass('active');
    //             setTimeout(function () {
    //                 parent.remove();
    //             }, 300);
    //             $.cookie('isSecond','true');
    //             return false;
    //         }
    //     }else{
    //
    //         setTimeout(function(){
    //             header.removeClass('active');
    //             nav.removeClass('active');
    //         },700);
    //
    //         skip.remove();
    //     }
    //
    // }();

    //选择章节
    function muluload () {

        var touchD, orientation, touchX, touchY, DX, DY,
            li = catalogue.find('li');
        li.on('touchstart', function (event) {
            touchD = 0; //恢复触屏误差值

            var touch = event.targetTouches[0];

            touchX = touch.clientX;
            touchY = touch.clientY;
        });

        li.on('touchmove', function (event) {

            var touch = event.targetTouches[0];
            var x = touch.clientX;
            var y = touch.clientY;
            orientation = x - touchX; //方向，正数为右，负数为左；
            DX = Math.abs(orientation);
            DY = Math.abs(y - touchY);

            touchD = DX > DY ? DX : DY; //计算差值
        });

        li.on('touchend', function () {
            console.log('选择章节');
            if (touchD < 7) {
                var index = parseInt($(this).data('index'));
                _.selectChapter(index, updatePageTitle);
                catalogue.removeClass('active');

            } else if (DX > DY && orientation < 0) { //关闭目录
                catalogue.removeClass('active');
            }

        })
    }

    //更新页眉
    function updatePageTitle() {
        var h3 = $('#book-pageinfo').find('h3');
        var nowPage = $('#book-pageinfo').find('b.book-nowpage');
        var countPage = $('#book-pageinfo').find('b.book-countpage');
        var catalogUl = catalogue.find('ul');
        var data = _.getPageData();
        h3.html(data.chapterName);
        nowPage.html(data.pageNum);
        countPage.html(data.pageCount);
        var activeLi = catalogUl.find('li.active');
        var index = activeLi.data('index');

        if (parseInt(index) !== data.nowChapter) {  //目录变色
            activeLi.removeClass('active');
            catalogUl.find('[data-index="' + data.nowChapter + '"]').addClass('active');
        }

        //活动字号
        selectSize.find('li[data-fontsize="'+data.fontSize+'"]').addClass('active');
    }




    //更改字号


    selectSize.find('li').on('touchend',function(){
        var li = $(this);

        if(!li.hasClass('active')){
            var size = li.data('fontsize');
            selectSize.find('.active').removeClass('active');
            li.addClass('active');
            _.ModFontSize(size)
        }

    })

});

